﻿/// <reference path="Panel.js" />

//可关闭的盒子
IM.Control.CollapsiblePanel = function(conf) {
    IM.Control.Panel.apply(this, arguments);
    this.Original; //原始信息
    this.Direction; //关闭方向
    this.Width; //关闭后最小宽度
    var _iMTimer = new IM.Timer();

    this.CollapsibleInit = function() {//初始化
        this.Render.css("overflow", "hidden");

        this.Original = new Object(); //拷贝Render的数据到Original备份,只拷备到Render第二级属性值
        for (var i in this.Render) {
            if (typeof this.Render[i] == "object") {
                this.Original[i] = new Object();
                for (var j in this.Render[i]) {
                    this.Original[i][j] = this.Render[i][j];
                }
            }
            else {
                this.Original[i] = this.Render[i];
            }
        }

        if (!this.Width)//如果没有给最小宽度则设置为0
            this.Width = 0;
    };

    //隐藏面版 隐藏时间(填时间)
    this.Hide = function(time, callback) {
        if (this.Direction == "left") {
            HideToLeft(this, time, callback);
        }
    };
    //显示面板 展开时间(填时间)
    this.Show = function(time, callback) {
        if (this.Direction == "left") {
            RestoreFromLeft(this, time, callback);
        }
    };

    //向左隐藏
    var HideToLeft = function(obj, time, callback) {

        if (obj.Render.width <= obj.Width)//已经隐藏了就不用再合闭
            return;

        if (!time)
            time = 1000;
        var left = obj.Render.width() - obj.Width;
        var speed = left * 33 * obj.Original.width() / (time * obj.Render.width());  
        _iMTimer.Start(33, time,
            function() { obj.Render.css("width", obj.Render.width() - speed < obj.Width ? obj.Width : obj.Render.width() - speed); },
            function() { obj.Render.css("width", obj.Width); if (callback) callback(); }
        );
    };

    //从左边恢复
    var RestoreFromLeft = function(obj, time, callback) {

        if (obj.Render.width() >= obj.Original.width())//已经展开不用再展开
            return;

        if (!time)
            time = 1000;
        var left = obj.Original.width() - obj.Width;
        var speed = left * 33 * obj.Original.width() / (time * (obj.Original.width() - obj.Render.width()));
        _iMTimer.Start(33, time,
            function() { obj.Render.css("width", obj.Render.width() + speed > obj.Original.width() ? obj.Original.width() : obj.Render.width() + speed); },
            function() { obj.Render.css("width", obj.Original.width()); if (callback) callback(); }
        );
    };

    this.CollapsibleInit();
};